﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kugar.Core.ExtMethod;
using Kugar.ERPFramework.Entity;

namespace Kugar.ERPFramework.View.WinForm
{
    public partial class RMSEditor : Form
    {
        private Form _baseFrm = null;
        private IEnumerable<ControlRMSItem> _currentRMSList = null;
        private List<ControlRMSItem> _tempRMSList = null;
        protected RMSEditor()
        {
            InitializeComponent();
        }

        public RMSEditor(Form frm,IEnumerable<ControlRMSItem> initRMSList  ):this()
        {
            _baseFrm = frm;
            _currentRMSList = initRMSList;

            _tempRMSList = backupRMSList(initRMSList);

            init();
        }

        private void init()
        {
            var rootNode = new TreeNode();

            rootNode.Text = this._baseFrm.Text;

            buildControlTree(_baseFrm, rootNode);

            treeView1.Nodes.Clear();

            treeView1.Nodes.Add(rootNode);
        }

        private void buildControlTree(Control parent,TreeNode parentNode)
        {
            foreach (Control control in parent.Controls)
            {
                if (string.IsNullOrWhiteSpace(control.Name))
                {
                    continue;
                }

                var node = new TreeNode();

                node.Name = control.Name;
                node.Text = string.IsNullOrWhiteSpace(control.Text) ? control.Name : control.Text;

                parentNode.Nodes.Add(node);

                if (control is DataGridView)
                {
                    var dg = (DataGridView) control;

                    for (int i = 0; i < dg.Columns.Count; i++)
                    {
                        var column = dg.Columns[i];

                        if (string.IsNullOrWhiteSpace(column.Name))
                        {
                            continue;
                        }

                        var tempNode = new TreeNode();
                        tempNode.Name = column.Name;
                        tempNode.Text = string.IsNullOrWhiteSpace(column.HeaderText) ? column.Name : column.HeaderText;

                        node.Nodes.Add(tempNode);
                    }
                }

                if ( control.HasChildren)
                {
                    buildControlTree(control,parentNode);
                }
                else
                {
                    
                }
            }
        }

        public List<ControlRMSItem> RMSList
        {
             get { return _tempRMSList; }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (dataGridView1.Tag!=null)
            {
                var config = (ControlRMSItem) dataGridView1.Tag;
                if (dataGridView1.Rows.Count>0)
                {
                    var tempRmsNames = new List<string>(dataGridView1.Rows.Count);

                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        var rmsName = row.Cells[0].Value.ToStringEx();

                        if (string.IsNullOrWhiteSpace(rmsName))
                        {
                            continue;
                        }

                        tempRmsNames.Add(rmsName);
                    }

                    config.RmsNames = tempRmsNames.ToArray();

                }
                else
                {
                    config.RmsNames = null;
                }

                config.WhenFaild = (ControlRMSItem.ControlRMSFaildHandle)comboBox1.SelectedIndex;

                if (config.RmsNames==null || config.RmsNames.Length<=0)
                {
                    _tempRMSList.Remove(config);
                }
                else
                {
                     _tempRMSList.Add(config);
                }
            }



            if (e==null || string.IsNullOrWhiteSpace(e.Node.Name))
            {
                dataGridView1.Tag = null;
                dataGridView1.Enabled = false;
                comboBox1.Enabled = false;
                return;
            }

            dataGridView1.Enabled = true;
            comboBox1.Enabled = true;
            dataGridView1.Rows.Clear();

            var configs = _tempRMSList.FirstOrDefault(x => x.ControlName == e.Node.Name);

            if (configs==null)
            {
                configs=new ControlRMSItem();
                configs.ControlName = e.Node.Name;
            }

            dataGridView1.Tag = configs;

            if (configs.RmsNames!=null)
            {
                for (int i = 0; i < configs.RmsNames.Length; i++)
                {
                    if ( string.IsNullOrWhiteSpace(configs.RmsNames[i]))
                    {
                        continue;
                    }
                    dataGridView1.Rows.Add(configs.RmsNames[i]);
                }                
            }

            comboBox1.SelectedIndex =(int) configs.WhenFaild;
        }

        private List<ControlRMSItem> backupRMSList(IEnumerable<ControlRMSItem> srcList) 
        {
           var _temp = new List<ControlRMSItem>();

            if (srcList!=null)
            {
                foreach (var item in srcList)
                {
                    _temp.Add((ControlRMSItem)item.Clone());
                }
            }

            return _temp;
        }

        private void btnRecover_Click(object sender, EventArgs e)
        {
            _tempRMSList = backupRMSList(_currentRMSList);

        }

        private void btnSum_Click(object sender, EventArgs e)
        {
            var rmsNameList = new HashSet<string>();

            foreach (var rmsItem in _tempRMSList)
            {
                foreach (var rmsStr  in rmsItem.RmsNames)
                {
                    rmsNameList.Add(rmsStr);
                }
            }

            //TODO:输出rmsNameList到指定的xml文件中进行汇总
        }

        private void dataGridView1_Leave(object sender, EventArgs e)
        {
            treeView1_NodeMouseClick(this,null);
        }
    }
}
